package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.Machine;
import com.hzhc.mapper.MachineMapper;
import com.hzhc.service.MachineService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.MachineVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class MachineServiceImpl extends ServiceImpl<MachineMapper, Machine> implements MachineService {
    @Override
    public List<Machine> getList(MachineVo vo, Page<Machine> pagination) {
        IPage<Machine> iPage = baseMapper.selectPage(pagination, Wrappers.<Machine>lambdaQuery()
        .eq(Machine::getDisable,false)
                .like(StringUtils.isNotBlank(vo.getMachineNo()), Machine::getMachineNo, vo.getMachineNo())
                .like(StringUtils.isNotBlank(vo.getMachineName()), Machine::getMachineName, vo.getMachineName())
                .like(StringUtils.isNotBlank(vo.getUserName()), Machine::getUserName, vo.getUserName())
                .orderByDesc(Machine::getCreateTime)
        );
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addRet(Machine machine) {
        machine.setDisable(false).setCreateTime(LocalDateTime.now());
        boolean b =save(machine);
        return b?ResultUtil.ok():ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateRet(Machine vo) {
        boolean b = updateById(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateDelete(Machine vo) {
        //0显示 1不显示
        vo.setDisable(true);
        boolean b = updateById(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public Machine checkMachine(String field, String name, Long id) {
        QueryWrapper<Machine> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("disable",false);
        queryWrapper.eq(field,name);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        Machine machine= baseMapper.selectOne(queryWrapper);
        return machine;
    }

    @Override
    public List<Machine> getAllMachineList(Machine machine) {
        List<Machine> list = list(Wrappers.<Machine>lambdaQuery()
                .eq(Machine::getDisable,0)
        );
        return list;
    }
}
